SwiftUI を内包する UITabBarItem.title が画面遷移で変わってしまう
UITabBarController>[UINavigationController]>UIHostingController>NavigationLink- という構成において、
NavigationLinkが発火するとtabBarItem.titleが消えてしまうというバグ?に遭遇した。
swift - UITabBar containing SwiftUI View - Stack Overflow
- 多分これが該当する issue で、 iOS のバグって書いてある。
UITabBarController が保持している UINavigationController を以下の class に置き換えたら修正された。
// Workaround: SwiftUI.NavigationLink による遷移で tabBarItem.title が無くなるバグの対応
// see: https://stackoverflow.com/a/71929880
final class TabBarItemStoringNavigation: UINavigationController {
private var storedTabBarItem: UITabBarItem?
override var tabBarItem: UITabBarItem! {
get { storedTabBarItem ?? super.tabBarItem }
set { storedTabBarItem = newValue }
}
}
tabBarItem をセットすれば storedTabBarItem に自動でセットされるので、特に外から storedTabBarItem に明示的にセットしたり、 init で渡すといったことは不要だった。